package terra.model.object.action;

import terra.model.object.ai.AbstractAI;

public class ActionMove extends AbstractAction {

	private static final long serialVersionUID = 6728633910541554326L;
	public float x, y, distance;
	public boolean allowJump = true;
	public boolean moveLeft = false;
	int maxTime = 10000;

	public ActionMove(AbstractAI ai, float x, float y, boolean canJump) {
		super(AbstractAI.ACTION_MOVE, ai);
		this.x = x;
		this.y = y;
		distance = Math.abs(x - ai.getOwner().getX());
		allowJump = canJump;
		moveLeft = ai.getOwner().isLeft();
	}

	public ActionMove(AbstractAI ai, float distance, boolean canJump) {
		super(AbstractAI.ACTION_MOVE, ai);
		x = ai.getOwner().getX() + distance;
		y = -1;
		this.distance = Math.abs(distance);
		allowJump = canJump;
		moveLeft = ai.getOwner().isLeft();
	}

	@Override
	public void update(int delta) {
		maxTime -= delta;
		float diffX;
		if (ai.getOwner().isLeft())
			diffX = -ai.getOwner().getStat().getMoveSpeed() / 1000f * delta;
		else
			diffX = ai.getOwner().getStat().getMoveSpeed() / 1000f * delta;

		if (!ai.getOwner().isCollision2(ai.getOwner().getX() + diffX,
				ai.getOwner().getY()))
			ai.getOwner().setX(ai.getOwner().getX() + diffX);
		else if (allowJump)
			ai.getOwner().setYacceleration(-ai.getOwner().getJumpStartSpeed());
		else
			end();

		if (Math.abs(x - ai.getOwner().getX()) < 5
				&& (y == -1 || Math.abs(y - ai.getOwner().getY()) < 5))
			end();
		if (maxTime <= 0)
			end();

	}

}
